﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using DTO;
using System.Drawing;

namespace WebChamCong
{
    public partial class CheckInOuts : System.Web.UI.Page
    {
        private GridViewHelper _helper;

        private DateTime _currentTime;

        private int _currentDepart;

        protected void Page_Load(object sender, EventArgs e)
        {
            Set_Group();

            if (!IsPostBack)
            {
                this._currentTime = DateTime.Now;

                Load_Departments();

                Load_Month(this._currentTime);

                Load_Year(this._currentTime);
                string str1 = Session["departmentID"].ToString();
                string [] str = str1.Split('&');
                if (str.Length == 1)
                {
                    DTO.RoleDTO dd = BUS.RolesBUS.getRoleByID(int.Parse(str[0]));
                    if (dd.UserRoles != null)
                    {
                        DTO.BoPhanDTO bp = BUS.BoPhanBUS.GetBoPhanById(int.Parse(dd.UserRoles));
                        Load_Histories(bp.DepartmentID, this._currentTime);
                    }
                    else
                    {
                        Load_Histories(Convert.ToInt32(this.ddl_listDepartment.SelectedItem.Value), this._currentTime);
                    }
                }
                
                return;
            }

            int localDepart = Convert.ToInt32(this.ddl_listDepartment.SelectedItem.Value);

            this._currentDepart = localDepart;
            
            int month = Convert.ToInt32(this.ddl_month.SelectedItem.Value);
            int year = Convert.ToInt32(this.ddl_year.SelectedItem.Value);
            int day = 1;

            DateTime localTime = new DateTime(year, month, day);

            Load_Histories(localDepart, localTime);
        }

        protected void Set_Group()
        {
            try
            {
                _helper = new GridViewHelper(this.gv_listCheckInOuts);
                _helper.RegisterGroup("UserName", true, true);
                _helper.GroupHeader += new GroupEvent(_helper_GroupHeader);
                _helper.ApplyGroupSort();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        protected void _helper_GroupHeader(string groupName, object[] values, GridViewRow row)
        {
            if (groupName.Equals("UserName"))
            {
                row.BackColor = Color.LightGray;
                row.ForeColor = Color.WhiteSmoke;
            }
        }

        protected void Load_Histories(int depart, DateTime time)
        {
            int localDepart = depart;
            DateTime localTime = time;
            List<CheckDetails> historiesByDepartAndDate = BUS.LichBUS.GetHistoriesByDepartAndDate(localDepart, localTime);

            this.gv_listCheckInOuts.DataSource = historiesByDepartAndDate;
            this.gv_listCheckInOuts.DataBind();
           
        }

        protected void Load_Departments()
        {
             string str1 = Session["departmentID"].ToString();
                string [] str = str1.Split('&');
                if (str.Length == 1)
                {
                    DTO.RoleDTO dd = BUS.RolesBUS.getRoleByID(int.Parse(str[0]));
                    if (dd.UserRoles == null)
                    {
                        // Get departments in database
                        List<DTO.BoPhanDTO> departments = BUS.BoPhanBUS.GetBoPhans();

                        this.ddl_listDepartment.Items.Clear();

                        foreach (var departItem in departments)
                        {
                            this.ddl_listDepartment.Items.Add(new ListItem(departItem.DepartmentName, departItem.DepartmentID.ToString()));
                        }

                        this.ddl_listDepartment.SelectedIndex = 0;
                    }
                    else
                    {
                        DTO.BoPhanDTO bp = BUS.BoPhanBUS.GetBoPhanById(int.Parse(dd.UserRoles));
                        this.ddl_listDepartment.Items.Clear();
                        this.ddl_listDepartment.Items.Add(new ListItem(bp.DepartmentName, bp.DepartmentID.ToString()));
                        this.ddl_listDepartment.SelectedIndex = 0;
                    }
                }
                else
                {
                    this.ddl_listDepartment.Items.Clear();
                    for (int i = 0; i < str.Length; i++)
                    {
                        DTO.RoleDTO dd = BUS.RolesBUS.getRoleByID(int.Parse(str[i]));
                        DTO.BoPhanDTO bp = BUS.BoPhanBUS.GetBoPhanById(int.Parse(dd.UserRoles));
                        this.ddl_listDepartment.Items.Add(new ListItem(bp.DepartmentName, bp.DepartmentID.ToString()));
                    }
                    this.ddl_listDepartment.SelectedIndex = 0;
                }

        }

        protected void Load_Month(DateTime time)
        {
            DateTime localTime = time;

            this.ddl_month.Items.Clear();

            for (int i = 1; i < 13; i++)
            {
                this.ddl_month.Items.Add(new ListItem("Tháng " + i, i.ToString()));

                // Check if month 's current day has value
                if (localTime.Month == i)
                {
                    this.ddl_month.SelectedIndex = i - 1;
                }

            }
        }

        protected void Load_Year(DateTime time)
        {
            DateTime localTime = time;

            this.ddl_year.Items.Clear();

            for (int i = 2000; i < 2051; i++)
            {
                this.ddl_year.Items.Add(new ListItem("Năm " + i, i.ToString()));

                // Check if year 's current day has value
                if (localTime.Year == i)
                {
                    this.ddl_year.SelectedIndex = i - 2000;
                }

            }
        }

        protected void gv_listCheckInOuts_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gv_listCheckInOuts.PageIndex = e.NewPageIndex;
            this.gv_listCheckInOuts.DataBind();
        }

        protected void gv_listCheckInOuts_Sorting(object sender, GridViewSortEventArgs e)
        {

        }

    }
}